package org.proddevplm.business.project;

import java.util.HashMap;
import java.util.Map;

import org.proddevplm.business.api.IAdapter;
import org.proddevplm.business.project.constants.ProjectConstants;
import org.proddevplm.helpers.ManagerUtil;

public class ProjectAdapter implements IAdapter{

	@Override
	public Object adapt(int command, Map<String, Object> dataObject) {
		Object adaptedObject = null;
		Map<String, Object> projectMap = new HashMap<String, Object>();
		String userList;
		String[] usersArray;
		String rolesList;
		String[] rolesArrayAux;
		String[] rolesArray;
		
		switch (command){
		case ProjectConstants.ADD_PROJECT:
						
			projectMap.put("projectName", ManagerUtil.getStringFromReq(dataObject.get("projectName")));
			projectMap.put("projectDescription", ((String[])dataObject.get("description"))[0]);			
			projectMap.put("startDate", ManagerUtil.getStringFromReq(dataObject.get("startdatepicker")));		
			projectMap.put("endDate", ManagerUtil.getStringFromReq(dataObject.get("enddatepicker")));		
			projectMap.put("userlist", ManagerUtil.getStringFromReq(dataObject.get("userlist")));
			
			userList = (String)projectMap.get("userlist");
			usersArray = userList.split(",");
			projectMap.put("usersArray", usersArray);
						
			projectMap.put("roleslist", ManagerUtil.getStringFromReq(dataObject.get("roleslist")));
			
			rolesList = (String)projectMap.get("roleslist");
			rolesArrayAux = rolesList.split(",");
			rolesArray = new String[rolesArrayAux.length];
			for(int i=0; i<rolesArrayAux.length; i++)
				rolesArray[i] = rolesArrayAux[i].trim();
			projectMap.put("rolesArray", rolesArray);
			
			adaptedObject = projectMap;
			break;
			
		case ProjectConstants.OPEN_PROJECT:
			projectMap.put("projectId", ManagerUtil.getStringFromReq(dataObject.get("projectId")));
			adaptedObject = projectMap;
			break;
			
		case ProjectConstants.EDIT_PROJECT:

            projectMap.put("editProjectName", ManagerUtil.getStringFromReq(dataObject.get("editProjectName")));
			projectMap.put("editProjectDescription", ((String[])dataObject.get("editDescription"))[0]);
			projectMap.put("editStartDate", ManagerUtil.getStringFromReq(dataObject.get("editStartDate")));
			projectMap.put("editEndDate", ManagerUtil.getStringFromReq(dataObject.get("editEndDate")));
			projectMap.put("editUserlist", ManagerUtil.getStringFromReq(dataObject.get("editUserlist")));
            projectMap.put("editRoleslist", ManagerUtil.getStringFromReq(dataObject.get("editRoleslist")));
		
			userList = (String)projectMap.get("editUserlist");
			usersArray = userList.split(",");
			projectMap.put("editUsersArray", usersArray);
			
			rolesList = (String)projectMap.get("editRoleslist");
			rolesArrayAux = rolesList.split(",");
			rolesArray = new String[rolesArrayAux.length];
			for(int i=0; i<rolesArrayAux.length; i++)
				rolesArray[i] = rolesArrayAux[i].trim(); //consider eliminating this line
			projectMap.put("editRolesArray", rolesArray);
			
					
			adaptedObject = projectMap;
			break;
		}
		
		return adaptedObject;
	}

}
